package weaver.formmode.customjavacode.modeexpand.srm.cg;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.modeexpand.srm.util.SrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 工程类需求拆分新建模板的新建保存
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/23
 */
public class WeaGcxqcfNewSaveAction extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    RecordSet rs = new RecordSet();
                    RecordSet rs1 = new RecordSet();
                    rs.writeLog( "WeagcxqcfNewSaveAction-----------------" + billid );
                    String cglx = "1";
                    String cgxqTable = SrmUtil.getCgxqTableType( cglx );
                    String cgrwTable = SrmUtil.getCgrwTableType( cglx );
                    String xqcfTable = SrmUtil.getXqcfTableType( cglx );
                    String comFields = "xmid,xqly,xqdjr,xqdw,sfjcxq,xqrq,xqlx,xmmc,xmbh,xmxz,\n" +
                            "fbbd,fblx,fbnrms,yjjcrq,yjtcsj,ygldlxq,tjfbs,ygje,sfbq,gyssl,\n" +
                            "fj,xqdh,xqbh,bgdh,bgbh,htdh,fplx,xqfl,jhdbjcsj,cgy,\n" +
                            "cgfs,pbfs,bz,fbzyej,pid,bmid,fbid,ppfw,bglsh,xmxzid,\n" +
                            "xqdwid,xqdwmc,jbgsl,cgqx,xmdd";
                    //插入主表
                    String sql = "select " + comFields + ",zcdbjcsj,xqdid,srmjcdh,fbmc from " + xqcfTable + " where id=?";
                    rs.executeQuery( sql, billid );
                    if (rs.next()) {
                        String xmid = Util.null2String( rs.getString( "xmid" ) );//项目ID
                        String xqly = Util.null2String( rs.getString( "xqly" ) );//需求来源
                        String xqdjr = Util.null2String( rs.getString( "xqdjr" ) );//需求对接人
                        String xqdw = Util.null2String( rs.getString( "xqdw" ) );//需求单位ID
                        String sfjcxq = Util.null2String( rs.getString( "sfjcxq" ) );//是否集采需求
                        String xqrq = Util.null2String( rs.getString( "xqrq" ) );//需求日期
                        String xqlx = Util.null2String( rs.getString( "xqlx" ) );//需求类型
                        String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
                        String xmbh = Util.null2String( rs.getString( "xmbh" ) );//项目编号
                        String xmxz = Util.null2String( rs.getString( "xmxz" ) );//项目性质
                        String fbbd = Util.null2String( rs.getString( "fbbd" ) );//分包标段
                        String fblx = Util.null2String( rs.getString( "fblx" ) );//分包类型
                        String fbnrms = Util.null2String( rs.getString( "fbnrms" ) );//分包内容描述
                        String yjjcrq = Util.null2String( rs.getString( "yjjcrq" ) );//预计进场日期
                        String yjtcsj = Util.null2String( rs.getString( "yjtcsj" ) );//预计退场时间
                        String ygldlxq = Util.null2String( rs.getString( "ygldlxq" ) );//预估劳动力需求
                        String tjfbs = Util.null2String( rs.getString( "tjfbs" ) );//推荐分包商
                        String ygje = Util.null2String( rs.getString( "ygje" ) );//预估金额
                        String sfbq = Util.null2String( rs.getString( "sfbq" ) );//是否标前
                        String gyssl = Util.null2String( rs.getString( "gyssl" ) );//供应商数量
                        String fj = Util.null2String( rs.getString( "fj" ) );//附件
                        String xqdh = Util.null2String( rs.getString( "xqdh" ) );//需求单号
                        String xqbh = Util.null2String( rs.getString( "xqbh" ) );//需求包号
                        String bgdh = Util.null2String( rs.getString( "bgdh" ) );//变更单号
                        String bgbh = Util.null2String( rs.getString( "bgbh" ) );//变更包号
                        String htdh = Util.null2String( rs.getString( "htdh" ) );//合同单号
                        String fplx = Util.null2String( rs.getString( "fplx" ) );//分配类型
                        String xqfl = Util.null2String( rs.getString( "xqfl" ) );//需求分类
                        String jhdbjcsj = Util.null2String( rs.getString( "jhdbjcsj" ) );//计划定标决策时间
                        String cgy = Util.null2String( rs.getString( "cgy" ) );//采购员
                        String cgfs = Util.null2String( rs.getString( "cgfs" ) );//采购方式
                        String pbfs = Util.null2String( rs.getString( "pbfs" ) );//评标方式
                        String bz = Util.null2String( rs.getString( "bz" ) );//备注
                        String fbzyej = Util.null2String( rs.getString( "fbzyej" ) );//分包专业（二级）
                        String pid = Util.null2String( rs.getString( "pid" ) );//PID
                        String bmid = Util.null2String( rs.getString( "bmid" ) );//部门ID
                        String fbid = Util.null2String( rs.getString( "fbid" ) );//分部id
                        String ppfw = Util.null2String( rs.getString( "ppfw" ) );//品牌范围
                        String bglsh = Util.null2String( rs.getString( "bglsh" ) );//变更流水号
                        String xmxzid = Util.null2String( rs.getString( "xmxzid" ) );//项目性质ID
                        String xqdwid = Util.null2String( rs.getString( "xqdwid" ) );//需求单位ID-树
                        String xqdwmc = Util.null2String( rs.getString( "xqdwmc" ) );//需求单位名称
                        String jbgsl = Util.null2String( rs.getString( "jbgsl" ) );//仅变更数量
                        String cgqx = Util.null2String( rs.getString( "cgqx" ) );//采购权限
                        String xmdd = Util.null2String( rs.getString( "xmdd" ) );//项目地点
                        String zcdbjcsj = Util.null2String( rs.getString( "zcdbjcsj" ) );//最迟定标决策时间
                        String xqdid = Util.null2String( rs.getString( "xqdid" ) );//需求单ID
                        String srmjcdh = Util.null2String( rs.getString( "srmjcdh" ) );//决策单号
                        String fbmc = Util.null2String( rs.getString( "fbmc" ) );//分包专业（一级）
                        String rwbh = SrmUtil.generTaskCode( cglx );//任务编号
                        String nd = xqrq.substring( 0, 4 );//年度
                        String cfdid = Util.null2String(billid);
                        String rwzt = "0";//任务状态
                        String formmodeid = SrmUtil.getFormmodeId( cglx );
                        String modedatacreatertype = "0";
                        String modedatacreater = user.getUID() + "";
                        String modedatacreatedate = DateUtil.getCurrentDate();
                        String fullDate = DateUtil.getFullDate();
                        String modedatacreatetime = fullDate.substring( 11, 19 );
                        String fields = comFields + ",zcdbjcsj,xqdid,cfdid,rwbh,nd,jcdh,fbzyyj,rwzt,cglx," +
                                "formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime";
                        String values = "values(" +
                                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                        String sql1 = "insert into " + cgrwTable + " (" + fields + ") " + values;
                        rs.writeLog( sql1 );
                        Object[] params = {xmid, xqly, xqdjr, xqdw, sfjcxq, xqrq, xqlx, xmmc, xmbh, xmxz,
                                fbbd, fblx, fbnrms, yjjcrq, yjtcsj, ygldlxq, tjfbs, ygje, sfbq, gyssl,
                                fj, xqdh, xqbh, bgdh, bgbh, htdh, fplx, xqfl, jhdbjcsj, cgy,
                                cgfs, pbfs, bz, fbzyej, pid, bmid, fbid, ppfw, bglsh, xmxzid,
                                xqdwid, xqdwmc, jbgsl, cgqx, xmdd,zcdbjcsj, xqdid, cfdid, rwbh, nd, srmjcdh, fbmc, rwzt,cglx,
                                formmodeid, modedatacreater, modedatacreatertype, modedatacreatedate, modedatacreatetime};
                        if (rs.executeUpdate( sql1, params )) {
                            //插入明细表
                            String comFieldDt1 = "fb,zfb,wlbm,wlmc,cms,xlh,xlbz,dw,sl,fzdw," +
                                    " fzsl,cgsl,sysl,fbbh,zfbbh,zfbid,wlid";
                            String mainid = SrmUtil.getRwbh2Id( rwbh, cglx );
                            SrmUtil.getNewModeRight(cgy,formmodeid,mainid);
                            String sql2 = "select id, " + comFieldDt1 + ",xqmxid from " + xqcfTable + "_dt1 where mainid=? and gxcf=1";
                            rs1.executeQuery( sql2, billid );
                            rs1.writeLog( sql2 );
                            while (rs1.next()) {
                                String id = Util.null2String( rs1.getString( "id" ) );//id
                                String fb = Util.null2String( rs1.getString( "fb" ) );//分部
                                String zfb = Util.null2String( rs1.getString( "zfb" ) );//子分部
                                String wlbm = Util.null2String( rs1.getString( "wlbm" ) );//物料编码
                                String wlmc = Util.null2String( rs1.getString( "wlmc" ) );//物料名称
                                String cms = Util.null2String( rs1.getString( "cms" ) );//长描述
                                String xlh = Util.null2String( rs1.getString( "xlh" ) );//序列号
                                String xlbz = Util.null2String( rs1.getString( "xlbz" ) );//序列备注
                                String dw = Util.null2String( rs1.getString( "dw" ) );//单位
                                String sl = Util.null2String( rs1.getString( "sl" ) );//数量
                                String fzdw = Util.null2String( rs1.getString( "fzdw" ) );//辅助单位
                                String fzsl = Util.null2String( rs1.getString( "fzsl" ) );//辅助数量
                                String cgsl = Util.null2String( rs1.getString( "cgsl" ) );//采购数量
                                String sysl = Util.null2String( rs1.getString( "sysl" ) );//剩余数量
                                String fbbh = Util.null2String( rs1.getString( "fbbh" ) );//分部编号
                                String zfbbh = Util.null2String( rs1.getString( "zfbbh" ) );//子分部编号
                                String zfbid = Util.null2String( rs1.getString( "zfbid" ) );//子分部ID
                                String wlid = Util.null2String( rs1.getString( "wlid" ) );//物料ID
                                String xqmxid = Util.null2String( rs1.getString( "xqmxid" ) );//需求明细ID
                                String cfbj = "1";
                                String sql3 = "insert into " + cgrwTable + "_dt1 (mainid," + comFieldDt1 + ",cfbj,xqmxid) " +
                                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                                Object[] params1 = {mainid, fb, zfb, wlbm, wlmc, cms, xlh, xlbz, dw, sl, fzdw,
                                        fzsl, cgsl, sysl, fbbh, zfbbh, zfbid, wlid, cfbj, xqmxid};
                                rs1.executeUpdate( sql3, params1 );
                                //更新物料采购需求池的勾选拆分
                                rs1.executeUpdate( "update " + cgxqTable + "_dt1 set cfbj=1 where id=?", id );
                            }
                            if(SrmUtil.getDtlCounts(xqdid,cglx)){
                                rs1.executeUpdate("update "+cgxqTable+" set fplx=1 where id in ("+xqdid+")");
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }

}